<?php
class Modules_Common_DB_Projects extends BaseObject
{
	public static function GetTableName() {
        return 'projects';
    }
    public static function &ById($id) {
        self::GetById($id);
    }
    public static function &SearchByUser($user)
    {
    	$projects=new Modules_Common_DB_Projects();
    	$array=array();
    	$objects=Modules_Common_DB_RTUserProject::ByUser($user);
    	foreach($objects as $value)
    		$array[]=$value->id_projects;
    	if(count($array)>0)		
    		return $projects->GetByIds($array);
    }
	 public static function &SearchByProject($id)
	    {
	    	$mcdu=new Modules_Common_DB_Users();
	    	$array=array();
	    	$objects=Modules_Common_DB_RTUserProject::ByProject($id);
	    	foreach($objects as $value)
	    		$array[]=$value->id_user;
	    	if(count($array)>0)		
	    		return $mcdu->GetByIds($array);
	    }
	public static function &SearchByWorkspace($account)
	    {
	    		$mcdt=new Modules_Common_DB_Projects();
	    	if(count(($ids=$mcdt->GetIds('WHERE workspace_id="'.Mysql::EscapeString($account).'"')))>0)
	        	return $mcdt->GetByIds($ids);
	        else
	        	return array();
	    }
	public function CreateNewMilestone($name,$start,$end)
	{
		$data=array("name"=>$name,
		"start"=>$start,
		"end"=>$end
		);
		$milestone=self::AddRecords($data,Modules_Common_DB_Milestones::GetTableName());
		$data=array(
			"projects_id"=>$this->id,
			"milestones_id"=>$milestone
		);
		self::AddRecords($data,Modules_Common_DB_ProjectHasMilestones::GetTableName());
	}
    
}